import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:short_drama/pages/mine/model/income_data_model.dart';
import 'package:short_drama/utils/colors.dart';

class EarningsIncomeDataWidget extends StatelessWidget {
  final List<IncomeDataModel> data;

  const EarningsIncomeDataWidget({super.key, required this.data});
  @override
  Widget build(BuildContext context) {
    double step = 154 / caluMaxValue();
    return Container(
      decoration: BoxDecoration(
        color: ClientColors.odffffff,
        borderRadius: BorderRadius.circular(8),
      ),

      height: 256,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SizedBox(height: 16),
          Text(
            "七天收益数据",
            style: TextStyle(
              color: Colors.white,
              fontSize: 16,
              fontWeight: FontWeight.bold,
            ),
          ).marginOnly(left: 7),
          SizedBox(height: 8),
          SizedBox(
            height: 186,
            // padding: EdgeInsets.only(bottom: 24),
            child: ListView.builder(
              scrollDirection: Axis.horizontal,
              itemCount: data.length,
              itemBuilder: (BuildContext context, int index) {
                IncomeDataModel model = data[index];
                double maxBarHeight = 154; // 你根据实际情况调整
                double rawHeight = step * double.parse(model.number ?? "0");
                double itemHeight =
                    rawHeight > maxBarHeight ? maxBarHeight : rawHeight;
                double space = (MediaQuery.of(context).size.width - 32) / 7;
                return SizedBox(
                  width: space, // 给宽度限制，避免宽度无限扩展
                  height: 186,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      Text(
                        model.number ?? "",
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 10,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      SizedBox(height: 4),
                      Container(
                        decoration: BoxDecoration(
                          color: ClientColors.oxFFD9D9D9,
                          borderRadius: BorderRadius.circular(4),
                        ),
                        height: itemHeight,
                        width: 8,
                      ),
                      SizedBox(height: 3),
                      Text(
                        model.date ?? "",
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 8,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ],
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }

  double caluMaxValue() {
    double maxValue = 0;
    for (var element in data) {
      double value = double.tryParse(element.number ?? "0") ?? 0;
      if (value > maxValue) {
        maxValue = value;
      }
    }
    return maxValue;
  }
}
